package com.cskaoyan.service;

import com.cskaoyan.bean.entity.*;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.common.util.StringUtils;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import javax.xml.ws.soap.Addressing;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public BasePageData query(String username, String mobile, BaseParam param) {
        //配置分页信息 → 在执行预编译过程，会帮你拼接分页的sql语句
        PageHelper.startPage(param.getPage(), param.getLimit());

        UserExample example = new UserExample();
        //构造条件 → List<Criterion> → and方法做的事情就是构造一个Criterion的对象添加到List里
        //如果username不为null，拼接条件 and username like '%%'
        UserExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (StringUtils.isNotEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        //设置排序
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<User> users = userMapper.selectByExample(example);

        return BasePageData.ok(users);
    }

    /**
     * 会员管理：根据id信息进行查询
     * @param id
     * @return
     */
    @Override
    public User queryid(Integer id) {
        //0.构建默认值，进行分页（暂时）
//        PageHelper.startPage(1,20);

//        //1.构造条件
//        UserExample example = new UserExample();
//        UserExample.Criteria criteria = example.createCriteria();
//        if(id!=null){
//            criteria.andIdEqualTo(id);
//        }

        //2.设置排序（根据构建的默认值进行排序）
//        example.setOrderByClause("add_time"+" "+"desc");

        //3.调用mapper执行
//        List<User> users = userMapper.selectByExample(example);
        User user = userMapper.selectByPrimaryKey(id);

        return user;
    }


    /**
     * git pull 造成的冲突：修改迁移部分*******************对应controller层*********************
     */
    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    Search_historyMapper search_historyMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Autowired
    AddressMapper addressMapper;


    /**
     * 会员收藏部分
     * @param userId
     * @param valueId
     * @param param
     * @return
     */
    @Override
    public BasePageData list(Integer userId, Integer valueId, BaseParam param) {
        //0.设置分页
        PageHelper.startPage(param.getPage(),param.getLimit());

        //1.构造条件
//        AdminExample example = new AdminExample();
//        AdminExample.Criteria criteria = example.createCriteria();
        CollectExample example = new CollectExample();
        CollectExample.Criteria criteria = example.createCriteria();

        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }

        //2.设置排序
        example.setOrderByClause(param.getSort()+" "+param.getOrder());

        //3.具体执行
        List<Collect> collects = collectMapper.selectByExample(example);

        return BasePageData.ok(collects);
    }

    /**
     * 会员足迹
     * @param userId
     * @param goodsId
     */
    @Override
    public BasePageData footprint(Integer userId, Integer goodsId, BaseParam param) {
        //0.设置分页
        PageHelper.startPage(param.getPage(),param.getLimit());

        //1.构造条件
        FootprintExample example = new FootprintExample();
        FootprintExample.Criteria criteria = example.createCriteria();

        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(goodsId!=null){
            criteria.andGoodsIdEqualTo(goodsId);
        }

        //2.设置排序
        example.setOrderByClause(param.getSort()+" "+param.getOrder());

        //3.具体执行
        List<Footprint> footprints = footprintMapper.selectByExample(example);

        return BasePageData.ok(footprints);
    }


    /**
     * 历史搜索
     * @param userId
     * @param keyword
     * @param param
     * @return
     */
    @Override
    public BasePageData history(Integer userId, String keyword, BaseParam param) {
        //0.分页
        PageHelper.startPage(param.getPage(),param.getLimit());
        //1.构造条件
        Search_historyExample example = new Search_historyExample();
        Search_historyExample.Criteria criteria = example.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword!=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }

        //2.设置排序
        example.setOrderByClause(param.getSort()+" "+param.getOrder());
        //4.
        List<Search_history> histories = search_historyMapper.selectByExample(example);
        return BasePageData.ok(histories);
    }


    /**
     * 意见反馈
     */
    @Override
    public BasePageData feedback(Integer id, String username, BaseParam baseParam) {
        //0.分页处理
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());

        //1.条件规定
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        if(username!=null && !"".equals(username)){
            criteria.andUsernameLike("%"+username+"%");
        }

        //2.排序处理
        example.setOrderByClause(baseParam.getSort()+" "+baseParam.getOrder());
        //3.具体查询
        List<Feedback> feedbacks = feedbackMapper.selectByExample(example);

        return BasePageData.ok(feedbacks);
    }

    @Override
    public BasePageData list(String name, Integer userId, BaseParam param) {
        //0.分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        //1.构造条件
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");  //每一次调用一个.andU...方法，就是往List中添加一个条件
        }
        if (userId != null ) {
            criteria.andUserIdEqualTo(userId);
        }
        //2.设置排序
        example.setOrderByClause(param.getSort()+" "+param.getOrder());
        //3.具体执行
        List<Address> addresses = addressMapper.selectByExample(example);
        return BasePageData.ok(addresses);
    }

    @Override
    public int getUserId(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        if(username != null && !username.equals("")){
            userExampleCriteria.andUsernameEqualTo(username);
        }
        List<User> userList = userMapper.selectByExample(userExample);
        Integer userId = userList.get(0).getId();
        return userId;
    }
}
